<?php

namespace plugins\fund\api_module;

use plugins\fund\model\FundModel;
use plugins\pay\lib\Pay;
use think\Db;
use think\Validate;

class Record
{

    /**
     * 支付
     */
    public function pay($data = [])
    {
        $validate = new Validate([
            'uid'      => 'require',
            'pay_type' => 'require',
            'money'    => 'require',
        ]);
        if (!$validate->check($data)) {
            return zy_sys_echo(false, $validate->getError(), null, -1);
        }
        $member = Db::name('member')->where('uid', $data['uid'])->find();
        if (empty($member)) {
            return zy_sys_echo(false, '用户不存在', null, -1);
        }
        $payType = $data['pay_type'];
        //流水
        $add = [
            'uid'      => $data['uid'],
            'trade_sn' => time() . rand(10000, 99999), //流水号
            'money'    => $data['money'],
            'pay_type' => $payType,
            'type'     => !empty($data['type']) ? $data['type'] : 1, //1.支出 2.退款 3.充值
            'status'   => 2, //1.成功 2.失败
            'other'    => !empty($data['other']) ? $data['other'] : '',
        ];

        $fund = new FundModel();
        //余额支付直接返回成功或失败
        if ($payType == 'balance') {
            //修改余额
            $add['status'] = 1;
            $f = $fund->editMoney($data['uid'], $data['money'], 2);
            if ($f['status'] != true) {
                return zy_sys_echo(false, $f['message'], null, -1);
            }
            //添加流水
            $record = $fund->addRecord($add);
            return zy_sys_echo(true, '支付成功', null, 200);
        } else {
            $validate = new Validate([
                'bizcontent' => 'require',
            ]);
            if (!$validate->check($data)) {
                return zy_sys_echo(false, $validate->getError(), null, -1);
            }
            
            $bizcontent = $data['bizcontent'];
            $res = (new \plugins\fund\common\Common())->createPay( $payType , $bizcontent );
            if (empty($res)) {
                return zy_sys_echo(false, '支付口令生成失败：请检查参数是否传递正确', null, -1);
            }
            //添加流水
            $record = $fund->addRecord($add);
            return zy_sys_echo(true, '成功', $res, 200);
        }
    }



    /**
     * 退款
     */
    public function refund($data = [])
    {
        $validate = new Validate([
            'uid'      => 'require',
            'money'    => 'require',
            'pay_type' => 'require',
        ]);

        if (!$validate->check($data)) {
            return zy_sys_echo(false, $validate->getError(), null, -1);
        }

        $member = Db::name('member')->where('uid', $data['uid'])->find();
        if (empty($member)) {
            return zy_sys_echo(false, '用户不存在', null, -1);
        }
        //添加流水
        $add = [
            'uid'      => $data['uid'],
            'trade_sn' => time() . rand(10000, 99999), //流水号
            'money'    => $data['money'],
            'pay_type' => $data['pay_type'],
            'type'     => 2, //1.支出 2.退款 3.充值
            'status'   => 1,
            'other'    => !empty($data['other']) ? $data['other'] : '',
        ];

        if ($data['pay_type'] == 'balance') {
            $fund = new FundModel();
            $f = $fund->editMoney($data['uid'], $data['money'], 1);
            if ($f['status'] != true) {
                return zy_sys_echo(false, '退款失败', null, -4);
            }
            $res = $fund->addRecord($add);
            return zy_sys_echo(true, '退款成功', null, 200);
        } else {
            return zy_sys_echo(false, '退款失败：目前仅支持退款到余额', null, -1);
        }
    }

    /**
     * 修改流水状态为成功
     */
    public function modifyStatus($out_trade_no, $transaction_id)
    {
        $da = [
            'status'         => 1,
            'transaction_id' => $transaction_id,
        ];

        $edit = Db::name('fund')->where('other', $out_trade_no)->update($da);
        return true;
    }

}
